#include "statistics_for_date.h"
#include "ui_statistics_for_date.h"

#include <QFont>
#include "define.h"
#include "log/log_init.h"
#include <dbio/db_manager.h>
StatisticsForDate::StatisticsForDate(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StatisticsForDate)
{
    ui->setupUi(this);
    view = new QChartView;
}

StatisticsForDate::~StatisticsForDate()
{
    delete ui;
}

void StatisticsForDate::CreateHistogram(HistogramType type, QString start, QString end)
{
    QLOG_DEBUG()<<"tableType = "<<tableType;
    switch (type) {
    case YearAndXForMonth:
        createYearAndXForMonth();
        break;
    case Month:
        createMonth();
        break;
    case AllDate:

        break;
    case Screen:

        break;
    case UserYser:

        break;
    case UserMonth:

        break;
    default:

        break;
    }
}

void StatisticsForDate::createOthers(QString chartname, QMap<QString, int> data, bool showCombobox)
{
    HistogramManager *chart = new HistogramManager;
    chart->setTitle(chartname);
    QStringList xAxis;//X轴坐标队列
    QBarSeries *series = new QBarSeries;
    QBarSet *set = new QBarSet("");//柱子大小
    QMap<QString, int>::iterator iter;
    for (iter = data.begin();iter != data.end();iter++) {
        xAxis.append(iter.key());
        *set<<iter.value();
    }
    chart->setAxisX(xAxis);
    int max = 0;
    for(auto it : data){
        max < it ?max = it : max ;
    }
    series->append(set);
    chart->setData(series,max);
    view->setChart(chart->getChart());
    view->setRenderHint(QPainter::Antialiasing);
    ui->verticalLayout->addWidget(view);
    if(!showCombobox)
        ui->comboBox->hide();
}

void StatisticsForDate::createOthers(QString chartname, QString start, QString end)
{
    QMap<QString,int> showData;

    QList<QList<QString>> data =  DBManager::getInstance()->select(QString(DEFAULT_SQL) + " where "+ QString("order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
    for(auto it : data){
        auto time = it.at(12);
        auto list = time.split("-");
        if(tableType == YearAndXForMonth||tableType == UserYser)
            showData[list.at(0)+"."+list.at(1)]++;//1.1
        else
            showData[list.at(0)+"."+list.at(1)+"."+list.at(2).mid(0,2)]++;
    }

    HistogramManager *chart = new HistogramManager;
    chart->setTitle(chartname);
    QStringList xAxis;//X轴坐标队列
    QBarSeries *series = new QBarSeries;
    QBarSet *set = new QBarSet("");//柱子大小
    QMap<QString, int>::iterator iter;
    for (iter = showData.begin();iter != showData.end();iter++) {
        xAxis.append(iter.key());
        *set<<iter.value();
    }
    int max = 0;
    for(auto it : showData){
        max < it ?max = it : max ;
    }

    chart->setAxisX(xAxis);
    series->append(set);
    chart->setData(series,max);
    view->setChart(chart->getChart());
    view->setRenderHint(QPainter::Antialiasing);
    ui->verticalLayout->addWidget(view);
}

void StatisticsForDate::createUsers(QString chartname ,QString start, QString end)
{
    QMap<QString,int> showData;
    QList<QList<QString>> data =  DBManager::getInstance()->select(QString(SELECT_USERNAME));

    for(auto it : data){
        QList<QList<QString>> data =  DBManager::getInstance()->select(QString(DEFAULT_SQL)
                                                                       +" WHERE cmf_user.user_nickname = '"+it.at(0)+"'" +" and "+
                                                                       QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
        QLOG_DEBUG()<<"createUsers SQL = "<<QString(DEFAULT_SQL)
                      +" WHERE cmf_user.user_nickname = '"+it.at(0)+"'" +" and "+
                      QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end);
        showData[it.at(0)] = data.size();
    }
    createOthers(chartname,showData,1);
}

void StatisticsForDate::setCombox(QSet<QString> date)
{
    disconnect(ui->comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(on_comboBox_currentIndexChanged(const QString &)));
    ui->comboBox->clear();
    for(auto it :date){
        ui->comboBox->addItem(it);
    }
    ui->comboBox->show();
    connect(ui->comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(on_comboBox_currentIndexChanged(const QString &)));
}

void StatisticsForDate::setNowTable(HistogramType table)
{
    tableType = table;
}

void StatisticsForDate::signOtherChart(QString chartname, QMap<QString, int> data)
{
    createOthers(chartname,data);
}

void StatisticsForDate::createYearAndXForMonth()
{

//    HistogramManager *chart = new HistogramManager;
//    chart->setTitle("年度订台表格");
//    chart->setAxisX(QStringList()<<"一月份"<<"二月份"<<"三月份"<<"四月份"<<"五月份"<<"六月份"<<"七月份");
//    QBarSeries *series = new QBarSeries;
//    QBarSet *set = new QBarSet("");
//    //如果一个X轴对一个柱，则X轴有多少 QBarSet都只有一个
//    *set<<qrand()%100<<qrand()%100<<qrand()%100<<qrand()%100<<qrand()%100<<qrand()%100<<qrand()%100;
//    series->append(set);

//    chart->setData(series);
//    view->setChart(chart->getChart());
//    view->setRenderHint(QPainter::Antialiasing);
//    ui->verticalLayout->addWidget(view);
}

void StatisticsForDate::createMonth()
{
    QDate date(QDate::currentDate());
    int month = date.month();
    int daysInMonth = date.daysInMonth();
    HistogramManager *chart = new HistogramManager;
    QStringList dayList;
    for(int i=1;i<daysInMonth+1;i++){
        dayList.append(QString("%1.%2").arg(month).arg(i));
    }
    chart->setTitle(QString("%1月订台表").arg(month));
    chart->setAxisX(dayList);
    QBarSeries *series = new QBarSeries;
    QBarSet *set = new QBarSet("");
    for(int i  =0 ; i<daysInMonth ;i++)
        *set<<qrand()%100;
    series->append(set);
    chart->setData(series);
    view->setChart(chart->getChart());
    view->setRenderHint(QPainter::Antialiasing);
    ui->verticalLayout->addWidget(view);

}

void StatisticsForDate::createEmployeeYear()
{

}

void StatisticsForDate::createEmployeeMonth()
{

}

void StatisticsForDate::hideUi()
{
    ui->comboBox->hide();
}

void StatisticsForDate::on_comboBox_currentIndexChanged(const QString &date)
{
    QString start,end,tablename;

    if(tableType == YearAndXForMonth||tableType == UserYser){
        start = date + YEAR_START;
        end = date + YEAR_END;
        tablename = date +"年度统计表格";
    }else{
        QDate time(date.split(".").at(0).toInt(),date.split(".").at(1).toInt(),1);
        auto days = time.daysInMonth();
        QString nDate(date);
        nDate.replace(".","-");
        start = nDate + MONTH_START;
        end = nDate + QString(MONTH_END).arg(days);
        tablename = date +"月度统计表格";
    }
    QLOG_DEBUG()<<"start = "<<start<<"   end = "<<end <<"tableType = "<<tableType;
    if(tableType == UserYser || tableType == UserMonth){
        createUsers(tablename ,start,end);
    }else{
        createOthers(tablename,start,end);
    }

}
